from absl import app, flags

import coin.proto.coin_feed_pb2 as proto_pb2
from coin.exchange.util.simple_record import RecordReader

FLAGS = flags.FLAGS

flags.DEFINE_string('filename', None, 'filename')

flags.DEFINE_bool('as_csv', False, 'as_csv')


def main(_):
  filename = FLAGS.filename

  if FLAGS.as_csv:
    print("HEADER")
  else:
    print("reading to " + filename)

  reader = RecordReader(filename)
  for record in reader.read_records():
    pb = proto_pb2.FeedMessage()
    pb.ParseFromString(record)
    if FLAGS.as_csv:
      print(pb.exchange_type,
            pb.symbol,
            pb.fetched_time,
            pb.request_time,
            pb.book.bid[0].price,
            pb.book.ask[0].price,
            sep=",")
    else:
      print(pb)


if __name__ == '__main__':
  app.run(main)
